<?php
function alertMes($mes, $url = '')
{
	if ($url != '') {
		exit("<script>alert('{$mes}');window.location='{$url}';</script>");
	} else {
		exit("<script>alert('{$mes}');history.back();</script>");
	}
}
/**
 * 获取验证码
 * @return img [description]
 */
function getVerification()
{
	$Verify = new \Think\Verify();
	$Verify->expire = 120;
	$Verify->useImgBg = true;
	$Verify->fontSize = 20;
	$Verify->length = 4;
	$Verify->imageW = 142;
	$Verify->imageH = 40;
	$Verify->useNoise = false;
	$Verify->bg = array(rand(0, 255), rand(0, 255), rand(0, 255));
	$Verify->entry();
}
function getClientIP()
{
	global $ip;
	if (getenv("HTTP_CLIENT_IP"))
		$ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR"))
		$ip = getenv("REMOTE_ADDR");
	else $ip = "Unknow";
	return $ip;
}
function sendEmail($name, $email, $content)
{
	require_once 'Public/plugins/swiftmailer/lib/swift_required.php';
	//发送邮件，QQ邮箱
	//配置邮件服务器，得到传输对象
	$transport = Swift_SmtpTransport::newInstance('smtp.qq.com',465,'ssl');
	//设置登录账号和密码
	$transport->setUsername('mr.reboot@qq.com');
	$transport->setPassword('hcvmbrtgbpjubfba');
	//得到发送邮件对象Swift_Mailer对象
	$mailer = Swift_Mailer::newInstance($transport);
	//得到邮件对象
	$message = Swift_Message::newInstance();
	//设置管理员的信息
	$message->setFrom(array('mr.reboot@qq.com' => 'Mr.Reboot'));
	//邮件发给谁
	$message->setTo(array($email => "{$name}"));
	//设置邮件主题
	$message->setSubject('Mr.Reboot');
	$message->setBody("{$content}",'text/html','utf-8');
	try {
		if ($mailer->send($message)) {
			return true;
		} else {
			return false;
		}
	} catch (Swift_ConnectionException $e) {
		alertMes("邮件发送错误" . $e->getMessage());
	}
}